package org.yu.team2023.flashdays.item;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.util.NonNullFunction;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.yu.team2023.flashdays.ITreasureXP;
import org.yu.team2023.flashdays.ModItemTags;
import org.yu.team2023.flashdays.registries.SimpleReg;

import java.util.*;
import java.util.function.BiConsumer;

public class TreasureUpgradeItem extends Item implements ITreasureXP {
    protected final int xp;
    protected static final Map<Integer,RegistryObject<TreasureUpgradeItem>> SIZE_MAP=new TreeMap<>((o1, o2) -> -Integer.compare(o1,o2)){};
    private TreasureUpgradeItem(Properties pProperties,int xp) {
        super(pProperties);
        this.xp=xp;
    }

    @Override
    public int getTreasureXp(ItemStack stack) {
        return xp*stack.getCount();
    }
    public static RegistryObject<TreasureUpgradeItem> create(SimpleReg reg,String name,String cn, int xp){
        RegistryObject<TreasureUpgradeItem> r= reg.item(name,properties -> new TreasureUpgradeItem(properties,xp)).lang(cn).simpleModel().build(ModItemTags.CURIOS_TREASURES_XP);
        SIZE_MAP.put(xp,r);
        return r;
    }
    public static List<ItemStack> xpToStack(int xp){
        List<ItemStack> results=new ArrayList<>();
        for (Map.Entry<Integer, RegistryObject<TreasureUpgradeItem>> entry : SIZE_MAP.entrySet()) {
            var exp=entry.getKey();
            var item=entry.getValue();
            int count=0;
            while (xp>=exp){
                xp-=exp;
                count++;
                if(count==64) {
                    results.add(new ItemStack(item.get(), count));
                    count = 0;
                }
            }
            if(count>0)results.add(new ItemStack(item.get(),count));
        }
        return results;
    }

    @Override
    public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
        super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced);
        pTooltipComponents.add(Component.translatable("tip.flashdays.treasure_upgrade_xp",xp).withStyle(ChatFormatting.GOLD,ChatFormatting.BOLD));
    }
}
